<%@ WebHandler Language="C#" Class="GetCusDocInfo" %>
using System;
using System.Web;
using LdExpress;
using Newtonsoft.Json;
using System.Data;
using System.Collections;

public class GetCusDocInfo : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        var action = context.Request.Params["action"];
        if (!string.IsNullOrEmpty(action))
        {
            switch (action.ToLower())
            {
                case "getsuggettion":
                    GetSuggestions(context);
                    break;
                case "getinfobyshprname":
                    GetCusDocInfoByShprName(context);
                    break;
            }
        }
    }
    private void GetSuggestions(HttpContext context)
    {
        string Key = "";
        string ColName = "";
        DataSet ds = null;
        if ((context.Request.QueryString.Get("Key") != "") && (context.Request.QueryString.Get("Key") != null))
        {
            Key = context.Request.QueryString.Get("Key");
        }
        if ((context.Request.QueryString.Get("ColumnName") != "") && (context.Request.QueryString.Get("ColumnName") != null))
        {
            ColName = context.Request.QueryString.Get("ColumnName");
        }
        string sqlCommand = "Select DISTINCT " + ColName + " from tbl_cusdoc where " + ColName + " like '%" + DBModule.RefineString(Key).Trim() + "%' limit 0,10";
        ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        if (ds != null && ds.Tables != null)
        {
            ArrayList source = new ArrayList();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                source.Add(row[0].ToString());
            }
            var str = JsonConvert.SerializeObject(source);
            context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(600));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Write(str);
        }
        else
        {
            context.Response.Write(JsonConvert.SerializeObject("empty"));
        }
    }
    private void GetCusDocInfoByShprName(HttpContext context)
    {
        string shprKey = "";
        DataSet ds = null;
        if ((context.Request.QueryString.Get("shprKey") != "") && (context.Request.QueryString.Get("shprKey") != null))
        {
            shprKey = context.Request.QueryString.Get("shprKey");
            //shprKey = HttpUtility.HtmlDecode(shprKey).Replace("\n", "<br>");
        }

        string sqlCommand = "Select * from tbl_cusdoc where REPLACE(HBLSHPR,'\r\n','') like '%" + DBModule.RefineString(shprKey).Trim() + "%' limit 0,1";
        ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ArrayList source = new ArrayList();
            object dynamic;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                dynamic = new
                {
                    ID = row["ID"].ToString(),
                    HBLSHPR = row["HBLSHPR"].ToString(),
                    HBLCNEE = row["HBLCNEE"].ToString(),
                    HBLNTF = row["HBLNTF"].ToString(),
                    MBLSHPR = row["MBLSHPR"].ToString(),
                    MBLCNEE = row["MBLCNEE"].ToString(),
                    MBLNTF = row["MBLNTF"].ToString(),
                    AGDLVR = row["AGDLVR"].ToString(),
                    CARRN = row["CARRN"].ToString(),
                };

                source.Add(dynamic);
            }

            var str = JsonConvert.SerializeObject(source);
            context.Response.Write(str);

        }
        else
        {
            context.Response.Write(JsonConvert.SerializeObject("empty"));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}